<?php
class RankStatisticService
{
	public function site($site_id,$type)
	{
		$site_name = DB::resultFirst('SELECT name FROM '.DB::table('site').' WHERE id = '.$site_id.' AND status = 1');
		if(!empty($site_name))
		{
			if($type == 'SITE_CLICK')
				$this->clickScoreHandler('site',$site_id,$site_name);
				
			$types = array(
					'SITE_CLICK'=>'click_count',
					'SITE_BEST'=>'best_count',
					'SITE_CUSTOM'=>'custom_count',
					'SITE_SCORE'=>'score',
					);
			
			if(array_key_exists($type,$types))
			{
				$filed = $types[$type];
				
				if($type == 'SITE_SCORE')
				{
					$avg_score = DB::resultFirst('SELECT AVG(score) FROM '.DB::table('message')." 
								WHERE status = 1 AND rec_id = '$site_id' AND rec_module = 'SiteMessage'");
					$avg_score = round($avg_score,1);
					DB::update('site',array('last_mtime'=>TIME_UTC,'score'=>$avg_score),'id = '.$site_id);
				}
				else
					DB::query('UPDATE '.DB::table('site')." SET $filed = $filed + 1 WHERE id = $site_id");
			}
		}
		/*//周统计
		$week = date("w");
		if($week == 0)
			$week = 7;
		$time = mktime(0, 0, 0,date("m"), date("d")-$week+1, date("Y")) - date('Z');
		
		$this->siteStatistic($site_id,$type,'week',$time);
		//月统计
		$time = mktime(0, 0, 0,date("m"),1, date("Y")) - date('Z');
		$this->siteStatistic($site_id,$type,'month',$time);
		
		//年统计
		$time = mktime(0, 0, 0,1,1, date("Y")) - date('Z');
		$this->siteStatistic($site_id,$type,'year',$time);*/
	}
	
	public function goods($goods_id,$type)
	{
		$goods_name = DB::resultFirst('SELECT name FROM '.DB::table('goods').' WHERE id = '.$goods_id.' AND status = 1');
		if(!empty($goods_name))
		{
			if($type == 'GOODS_CLICK')
				$this->clickScoreHandler('goods',$goods_id,$goods_name);
			
			$types = array(
					'GOODS_CLICK'=>'click_count',
					'GOODS_BEST'=>'best_count',
					'GOODS_COLLECT'=>'collect_count'
					);
			
			if(array_key_exists($type,$types))
			{
				$filed = $types[$type];
				DB::query('UPDATE '.DB::table('goods')." SET $filed = $filed + 1 WHERE id = $goods_id");
				DB::query('UPDATE '.DB::table('goods_now')." SET $filed = $filed + 1 WHERE id = $goods_id");
				$site_id = intval(DB::resultFirst('SELECT site_id FROM '.DB::table('goods_now').' WHERE id = '.$goods_id));
				if($site_id > 0)
				{
					if($type == 'GOODS_COLLECT')
						RankStatisticService::site($site_id,'SITE_CLICK');
						
					$max_goods = DB::fetchFirst("SELECT id,$filed FROM ".DB::table('goods_now')." 
												WHERE site_id = '$site_id'  
												ORDER BY $filed DESC,sort ASC,id DESC LIMIT 0,1");
					if($max_goods)
					{
						$type = 'SITE_MAX_'.$type;
						$statistic = DB::fetchFirst('SELECT * FROM '.DB::table('rank_statistic')."
													WHERE site_id = '$site_id' AND type = '$type'");
						
						$statistic['site_id'] = $site_id;
						$statistic['goods_id'] = $max_goods['id'];
						$statistic['nnum'] = $max_goods[$filed];
						$statistic['type'] = $type;
						$statistic['statistic_time'] = TIME_UTC;
						DB::insert('rank_statistic',$statistic,false,true);
					}
					
				}
			}
		}
		
		/*$time = mktime(0, 0, 0, date("m")  , date("d"), date("Y")) - date('Z');
		$this->goodsStatistic($goods_id,$type,$time);*/
	}
	
	private function goodsStatistic($goods_id,$type,$time)
	{
		$statistic = DB::fetchFirst('SELECT * FROM '.DB::table('rank_statistic')."
						WHERE rec_id = $goods_id AND type = '$type'");
		
		if($type == "GOODS_SCOREK")
		{
			$score = DB::resultFirst('SELECT IFNULL(AVG(score),1) FROM '.DB::table('message')."
						WHERE rec_id = $goods_id AND rec_module='Goods'");
		}
		
		if($statistic)
		{
			if($statistic['statistic_time'] != $time)
			{
				if($type == "GOODS_SCOREK")
				{
					DB::update('rank_statistic',
								array(
									'nnum'=>$score,
									'bnum'=>$statistic['nnum'],
									'statistic_time'=>$time
								),'id = '.$statistic['id']);
				}
				else
				{
					DB::update('rank_statistic',
								array(
									'nnum'=>$statistic['nnum'] + 1,
									'bnum'=>$statistic['nnum'],
									'statistic_time'=>$time
								),'id = '.$statistic['id']);
				}
			}
			else
			{
				if($type == "GOODS_SCOREK")
					DB::update('rank_statistic',array('nnum'=>$score),'id = '.$statistic['id']);
				else
					DB::update('rank_statistic',array('nnum'=>$statistic['nnum'] + 1),'id = '.$statistic['id']);
			}
		}
		else
		{
			$statistic['rec_id'] = $goodsID;
			$statistic['type'] = $type;
			$statistic['time_type'] = "today";
			
			if($type == "GOODS_SCOREK")
			{
				$statistic['nnum'] = $score;
				$statistic['bnum'] = 1;
			}
			else
				$statistic['nnum'] = 1;
				
			$statistic['statistic_time'] = $time;
			DB::insert('rank_statistic',$statistic);
		}
	}
	
	private function siteStatistic($site_id,$type,$time_type,$time)
	{
		$statistic = DB::fetchFirst('SELECT * FROM '.DB::table('rank_statistic')."
						WHERE rec_id = $site_id AND type = '$type' AND time_type = '$time_type'");
						
		if($type == "SITE_SCOREK")
		{
			$score = DB::resultFirst('SELECT IFNULL(AVG(score),1) FROM '.DB::table('message')."
						WHERE rec_id = $site_id AND rec_module='SiteMessage' AND create_time > $time");
		}
	
		if($statistic)
		{
			if($statistic['statistic_time'] != $time)
			{
				if($type == "SITE_SCOREK")
				{
					DB::update('rank_statistic',
								array(
									'nnum'=>$score,
									'bnum'=>$statistic['nnum'],
									'statistic_time'=>$time
								),'id = '.$statistic['id']);
				}
				else
				{
					DB::update('rank_statistic',
								array(
									'nnum'=>1,
									'bnum'=>$statistic['nnum'],
									'statistic_time'=>$time
								),'id = '.$statistic['id']);
				}
			}
			else
			{
				if($type == "SITE_SCOREK")
					DB::update('rank_statistic',array('nnum'=>$score),'id = '.$statistic['id']);
				else
					DB::update('rank_statistic',array('nnum'=>$statistic['nnum'] + 1),'id = '.$statistic['id']);
			}
		}
		else
		{
			$statistic['rec_id'] = $site_id;
			$statistic['type'] = $type;
			$statistic['time_type'] = $time_type;
			if($type == "SITE_SCOREK")
			{
				$statistic['nnum'] = $score;
				$statistic['bnum'] = 1;
			}
			else
				$statistic['nnum'] = 1;
			
			$statistic['statistic_time'] = $time;
			
			DB::insert('rank_statistic',$statistic);
		}
	}
	
	public function clickScoreHandler($type,$id,$name)
	{
		global $_FANWE;
		if($_FANWE['uid'] > 0 && $_FANWE['group_id'] == 1)
		{
			$cache_dir = FANWE_ROOT.'./public/click/'.$type.'/'.$_FANWE['uid'];
			$cache_path = $cache_dir.'/ids.fanwe';
			$time_path = $cache_dir.'/time.fanwe';
			
			$toady = toDate(TIME_UTC,'Ymd');
			$time_str = $toady.','.TIME_UTC;
			if(!file_exists($cache_dir))
			{
				@mkdir($cache_dir);
				@chmod($cache_dir, 0777);
				writeFile($time_path,$time_str);
			}
			else
			{
				$times = explode(',',file_get_contents($time_path));

				if(TIME_UTC - $times[1] < 30)
					return;
				
				if($toady > $times[0])
					writeFile($cache_path,'');
			}
			
			$is_empty = true;
			if(file_exists($cache_path))
			{
				$content = file_get_contents($cache_path);
				if(!empty($content))
					$is_empty = false;
				
				$ids = explode(',',$content);
				unset($content);
				if(in_array($id,$ids))
					return;
			}
			
			if(!$is_empty)
				$id_str = ','.$id;
			else
				$id_str = $id;
			
			if(writeFile($cache_path,$id_str,'a') && writeFile($time_path,$time_str))
			{
				$msg = sprintf(lang('user','user_'.$type.'_click_score'),$name);
				S("User")->updateUserScore($_FANWE['uid'],$type,'click',$msg,$id,0);
			}
		}
	}
}
?>